package com.mune.gulimall.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mune.common.utils.PageUtils;
import com.mune.common.utils.Query;
import com.mune.gulimall.member.dao.MemberDao;
import com.mune.gulimall.member.dao.MemberLevelDao;
import com.mune.gulimall.member.entity.MemberEntity;
import com.mune.gulimall.member.entity.MemberLevelEntity;
import com.mune.gulimall.member.exception.PhoneExistException;
import com.mune.gulimall.member.exception.UserNameExistException;
import com.mune.gulimall.member.service.MemberService;
import com.mune.gulimall.member.vo.MemberLoginVo;
import com.mune.gulimall.member.vo.UserRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {
  @Autowired
  MemberLevelDao memberLevelDao;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<MemberEntity> page = this.page(
            new Query<MemberEntity>().getPage(params),
            new QueryWrapper<MemberEntity>()
    );

    return new PageUtils(page);
  }

  @Override
  public void register(UserRegisterVo userRegisterVo) throws PhoneExistException, UserNameExistException {
    MemberEntity memberEntity = new MemberEntity();
    MemberLevelEntity levelEntity = memberLevelDao.getDefaultLevel();
    memberEntity.setLevelId(levelEntity.getId());
    checkPhoneUnique(userRegisterVo.getPhone());
    checkUserNameUnique(userRegisterVo.getUserName());
    memberEntity.setMobile(userRegisterVo.getPhone());
    memberEntity.setUsername(userRegisterVo.getUserName());
    BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    String encode = passwordEncoder.encode(userRegisterVo.getPassword());
    memberEntity.setPassword(encode);
    this.baseMapper.insert(memberEntity);
  }

  @Override
  public void checkPhoneUnique(String Phone) throws PhoneExistException {
    Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", Phone));
    if (count > 0) throw new PhoneExistException();
  }

  @Override
  public void checkUserNameUnique(String userName) throws UserNameExistException {
    Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", userName));
    if (count > 0) throw new UserNameExistException();
  }

  @Override
  public MemberEntity login(MemberLoginVo vo) {

    String loginacct = vo.getLoginacct();
    String password = vo.getPassword();
    MemberEntity entity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", loginacct).or().eq("mobile", loginacct));
    if (entity == null) return null;
    boolean flag = new BCryptPasswordEncoder().matches(password, entity.getPassword());
    if (!flag) return null;
    return entity;
  }
}